<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    type:''
  })
  interface formQuert {
    pool_id:number
    in_profit_loss:number
    in_profit_rate:number
    out_profit_rate:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    in_profit_rate: [{ required: true, message: "请输入进入礼物池回馈率", trigger: "blur" }],
    in_profit_loss: [{ required: true, message: "请输入进入礼物池钻石数", trigger: "blur" }],
    out_profit_rate: [{ required: true, message: "请输入移除钻石比例", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await luckDrawApi.wishWheelSetUserPoolConfig(form).then(()=>{
        ToastSuccess('礼物池阀值设置成功')
        loading.close()
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = async(query:any={})=> {
    const { type='', pool_name='', id=0, in_profit_loss, in_profit_rate, out_profit_rate } = query
    action.type = type
    Object.assign(form,{
      pool_id:id,
      in_profit_loss,
      in_profit_rate,
      out_profit_rate
    })
    action.title = `更新礼物池阀值：${pool_name}`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" prop="in_profit_rate" label="进入礼物池回馈率">
          <el-input type="number" v-model="form.in_profit_rate" placeholder="请输入进入礼物池回馈率" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="in_profit_loss" label="进入礼物池钻石数">
          <el-input type="number" v-model="form.in_profit_loss" placeholder="请输入进入礼物池钻石数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="out_profit_rate" label="移除钻石比例">
          <el-input type="number" v-model="form.out_profit_rate" placeholder="请输入移除钻石比例" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
